//
//  LBGetDataTool.m
//  loveBeen
//
//  Created by guofei on 2016/11/20.
//  Copyright © 2016年 loveBeen. All rights reserved.
//

#import "LBGetDataTool.h"
#import "DSHTTPClient.h"
#import "LBSuperMarketCategory.h"
#import "LBSuperMarketProducts.h"



static id _instance;
@implementation LBGetDataTool

+ (instancetype)sharedGetDatatool{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });

    return _instance;
}

- (void)getDataWithURLString:(NSString *)urlStr parameter:(NSDictionary *)parameter complete:(void(^)(NSArray * modelList, NSError *error, NSString *message))completeBlock{
  
    [DSHTTPClient postUrlString:urlStr withParam:parameter withSuccessBlock:^(NSDictionary * data) {
        // MARK: - 左侧数据
        //  左侧字典数组
        NSArray *categoryArray = data[@"data"][@"categories"];
        
        // 保存 model 模型的可变数组
        NSMutableArray<LBSuperMarketCategory *> *categoryArrayM = [NSMutableArray array];
        
        // 字典转模型 -> 模型数组
        categoryArrayM = [NSArray yy_modelArrayWithClass:[LBSuperMarketCategory class] json:categoryArray].mutableCopy;

        // MARK: - 右侧数据
        // 数组字典
        /*
        {   
            @"id1":@[ @{}, @{}, @{} ],
            @"id2":@[ @{}, @{}, @{} ],
            @"id3":@[ @{}, @{}, @{} ],
            @"id4":@[ @{}, @{}, @{} ],
        };
         */
        NSDictionary *productsDict = data[@"data"][@"products"];
     
        for (NSInteger i = 0; i < categoryArrayM.count; i++) {
            
            // 右侧字典数组
            NSArray<NSDictionary *> *tempArray = productsDict[categoryArrayM[i].id];
            
            categoryArrayM[i].productsModelList = [NSArray yy_modelArrayWithClass:[LBSuperMarketProducts class] json:tempArray].mutableCopy;
        
        }
        
        //bottomview 中 Block 回调
        if (completeBlock) {
            
            completeBlock(categoryArrayM.copy, nil, nil);
        }
        
    } withFailedBlock:^(NSError *error) {
        
        if (completeBlock) {
            completeBlock(nil, error, nil);
        }
    } withErrorBlock:^(NSString *message) {
        completeBlock(nil, nil, message);
    }];
    
}


@end
